//author:kakake
class tsAdminDao {
    public data = {
        //42.192.250.43
        //10.0.4.5
        dbConn: TSLobsterAPI.DbHelper.setdbconn('42.192.250.43', 'CloudCMS', 'sa', 'Lobster@123')
    }

    public getwebsitelist(pageInfo: object, searchstr: string,workId:any) {
        var dbparams = {
            searchstr: '%' + searchstr + '%',
            workId:workId
        }
        var strsql = "select [WebsiteId], [WebsiteCode], [Url], [Title], [Keywords], [Memo], [DelFlag], [WorkId],CONVERT(varchar(50), [CreateTime],20) CreateTime,CONVERT(varchar(50), [UpdateTime],20) UpdateTime from CMS_BaseWebsite \
         where WorkId=@workId";
        if (dbparams.searchstr != '') {
            strsql += " and (WebsiteCode like @searchstr or Title like @searchstr)";
        }
        TSLobsterAPI.Utils.TrackLog('getwebsitelist', strsql);
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo);
    }

    public insertwebsite(website: object) {
        website["delflag"] = 0;
        var strsql = "insert into CMS_BaseWebsite (WebsiteCode,Url,Title,Keywords,Memo,DelFlag,WorkId,CreateTime,UpdateTime) \
        values (@code,@url,@title,@keywords,@memo,@delflag,@WorkId,GETDATE(),GETDATE())";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, website);
    }

    public updatewebsite(website: object) {
        var strsql = "update CMS_BaseWebsite set Url=@url,Title=@title,Keywords=@keywords,Memo=@memo,UpdateTime=GETDATE() where WebsiteId=@websiteId";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, website);
    }

    public setwebsitestatus(param: any) {
        var strsql = "update CMS_BaseWebsite set DelFlag=@delFlag,UpdateTime=GETDATE() where WebsiteId=@websiteId";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, param);
    }

    public getnodelist(param: any) {
        var strsql = "select * from CMS_Node where WebsiteId =@websiteid and IsDel = 0 and parentID=-1 ORDER BY ParentID,NodeSort";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, param, null);
    }

    public disablenode(param: any) {
        var strsql = "UPDATE CMS_Node SET NodeStatus=@status WHERE NID=@nodeid";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, param);
    }

    public deletenode(param: any) {
        var strsql = "UPDATE CMS_Node SET IsDel=1,NodeSort=-1 WHERE NID=@nodeid";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, param);
    }

    public getnode(param: any) {
        var strsql = "select * from CMS_Node where NID=@nodeid";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, param);
    }

    public getexchangenode(param: any) {
        var strsql = "select top 1 * from CMS_Node where WebSiteId =@WebsiteId and ParentID=@parentid and IsDel = 0  ";
        if (param.movestatus == 0) {
            strsql += " and NodeSort<@oldsort order by NodeSort desc";//上移
        } else {
            strsql += " and NodeSort>@oldsort order by NodeSort";//下移
        }
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, param);
    }

    public updatenodesort(param: any) {
        var strsql = "UPDATE CMS_Node SET NodeSort=@nodesort WHERE NID=@nodeid";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, param);
    }

    public getnodeandparentnode(nodeid: any) {
        TSLobsterAPI.Utils.TrackLog("nodeid",nodeid);
        var node = null;
        var parentnode = null;
        if (nodeid > 0) {
            node = this.getnode({nodeid:nodeid});
            TSLobsterAPI.Utils.TrackLog("node",node);
            parentnode = this.getnode({nodeid:node.ParentID});
            TSLobsterAPI.Utils.TrackLog("parentnode",parentnode);
        }
        return { entity: node, parentEntity: parentnode };
    }

    public insertnode(param: any) {
        param.NodeSort = this.getmaxnodesort(param.ParentID, param.WebsiteId);
        param.WorkId = TSLobsterAPI.Program.GetWorkId();
        var strsql = "insert into CMS_Node (NodeName,NodeCode,NodeTitle,ParentID,NodeSort,NodeDesc,NodeStatus,NodeDepth,NodeIcon,AddTime,IsDel,WorkId,WebsiteId) \
        values (@NodeName,@NodeCode,'',@ParentID,@NodeSort,@NodeDesc,@NodeStatus,@NodeDepth,'',GETDATE(),0,@WorkId,@WebsiteId)";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, param);
    }

    public updatenode(param: any) {
        var strsql = "update CMS_Node set NodeName=@NodeName,NodeCode=@NodeCode,NodeDesc=@NodeDesc where NID=@NID";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, param);
    }

    public getmaxnodesort(parentid: any, websiteid: any) {
        var strsql = "select max(NodeSort) MaxSort from CMS_Node where ParentID=@parentid and WebsiteId=@websiteid";

        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { parentid: parentid, websiteid: websiteid });
        if (result.MaxSort == null) {
            return 1;
        }
        else {
            return parseInt(result.MaxSort) + 1;
        }
    }

    public getcontentlist(searchstr, nodeid, pageInfo) {
        var strsql = "select * from CMS_BaseContextManage where IsDel=0"
        if (nodeid > 0) {
            strsql += " And NodeId=@nodeid";
        }
        if (searchstr != "") {
            strsql += " And Title like @title";
        }
        var param = {
            nodeid: nodeid,
            title: '%' + searchstr + '%'
        }

        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, param, pageInfo);
    }

    public updatecontextstate(cid, state) {
        var strsql = "";
        var strsql2 = "";
        var strsql3 = "";
        if (state == 0) {
            strsql = "UPDATE CMS_BaseContextManage SET Status=0,IsExpectPublish=0 WHERE CID=@cid";
        }
        else {
            strsql = "UPDATE CMS_BaseContextManage SET Status=99,ReleaseTime=GETDATE() WHERE CID=@cid";
        }
        if (state == 0) {
            strsql2 = "UPDATE CMS_BaseContext SET Status = 0 where  CID =@cid and IsDel = 0";
        } else {
            strsql2 = "UPDATE CMS_BaseContext SET Status = 99,ReleaseTime = GETDATE() where  CID =@cid and IsDel = 0";
        }

        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { cid: cid });
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql2, { cid: cid });
    }

    public deletecontext(cid) {
        var strsql = "UPDATE CMS_BaseContextManage SET IsDel=1,LastUpdateTime=GETDATE() WHERE CID=@cid";
        var strsql2 = "UPDATE CMS_BaseContext SET IsDel = 1,LastUpdateTime = GETDATE() where  CID =@cid";

        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { cid: cid });
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql2, { cid: cid });
    }

    public getarticle(cid) {
        var strsql = "select * from CMS_BaseContextManage where CID=@cid";
        var strsql2 = "select * from CMS_U_ArticleManage where CID=@cid";

        var res = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { cid: cid });
        var res2 = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql2, { cid: cid });
        var obj = { cont: res, art: res2 };
    
        return obj;
    }

    public insertcontext(context) {
        context.WorkId = TSLobsterAPI.Program.GetWorkId();
        //插入管理表
        var strsql = "INSERT INTO CMS_BaseContextManage(Title,Intraduce,PicPath,IsTop,IsHot,Status,IsDel,NodeID,TotalHit,AddTime,WorkId,IsExpectPublish,ShareNum)\
            VALUES (@Title,@Intraduce,@PicPath,0,0,0,0,@NodeID,0,getdate(),@WorkId,0,0);SELECT @@IDENTITY AS CID";

        var res = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, context);
        if (res.CID != null) {
            strsql = "INSERT INTO CMS_U_ArticleManage(CID,U_Cont) VALUES(@CID,@U_Cont)";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { CID: res.CID, U_Cont: context.U_Cont });
        }
        context.CID = res.CID;
        //插入发布表
        strsql = "INSERT INTO CMS_BaseContext(CID,Title,Intraduce,PicPath,IsTop,IsHot,Status,IsDel,NodeID,TotalHit,AddTime,WorkId,ShareNum)\
            VALUES (@CID,@Title,@Intraduce,@PicPath,0,0,0,0,@NodeID,0,getdate(),@WorkId,0);SELECT @@IDENTITY AS ID";

        res = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, context);
        if (res.ID != null) {
            strsql = "INSERT INTO CMS_U_Article(ID,U_Cont) VALUES(@ID,@U_Cont)";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { ID: res.ID, U_Cont: context.U_Cont });
        }
    }

    public updatecontext(context) {
        context.WorkId = TSLobsterAPI.Program.GetWorkId();
        //更新管理表
        var strsql = "update CMS_BaseContextManage set Title=@Title,Intraduce=@Intraduce,PicPath=@PicPath where CID=@CID ";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, context);

        strsql = "update CMS_U_ArticleManage set U_Cont=@U_Cont  where CID=@CID";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, context);

        //发布状态插入发布表
        if (context.Status == 99) {
            //更新管理表为未发表
            strsql = "update CMS_BaseContextManage set Status=0 where CID=@CID";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { CID: context.CID });
            //插入新数据
            strsql = "INSERT INTO CMS_BaseContext(CID,Title,Intraduce,PicPath,IsTop,IsHot,Status,IsDel,NodeID,TotalHit,AddTime,WorkId,ShareNum)\
                VALUES (@CID,@Title,@Intraduce,@PicPath,0,0,98,0,@NodeID,0,getdate(),@WorkId,0);SELECT @@IDENTITY AS ID";
            var res = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, context);
            if (res.ID != null) {
                strsql = "INSERT INTO CMS_U_Article(ID,U_Cont) VALUES(@ID,@U_Cont)";
                TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { ID: res.ID, U_Cont: context.U_Cont });
            }
        }
        //更新最后一条发布表
        else {
            strsql = "select max(ID) as ID from CMS_BaseContext where CID=@CID";
            var res = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { CID: context.CID });

            strsql = "update CMS_BaseContext set Title=@Title,Intraduce=@Intraduce,PicPath=@PicPath where ID=@ID ";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { ID: res.ID, Title: context.Title, Intraduce: context.Intraduce,PicPath:context.PicPath });

            strsql = "update CMS_U_Article set U_Cont=@U_Cont  where ID=@ID";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { ID: res.ID, U_Cont: context.U_Cont });
        }
    }

    public getIndexSetting(websiteId, pageInfo) {
        var strsql = "select a.*,b.Title as WebSiteTitle from CMS_IndexSetting a \
        inner join CMS_BaseWebsite b\
        on a.WebSiteId=b.WebsiteId\
        where  a.websiteid=@websiteId";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { websiteId: websiteId }, pageInfo);
    }

    public getWebSiteListData(workId:any) {
        var strsql = "select WebSiteId,Title from CMS_BaseWebsite where  DelFlag=0 and WorkId=@workId";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, {workId:workId}, null);
    }

    public insertindexsetting(setting) {
        var strsql = "INSERT INTO CMS_IndexSetting(Title,Title_en,[Desc],Layout,TextBackgroundColor,TextWidth,TotalWidth,BackImage,Url,UrlType,UrlParamValue,WebSiteId,WorkId,Sort,DelFlag)VALUES \
            (@Title,@Title_en,@Desc,@Layout,@TextBackgroundColor,@TextWidth,@TotalWidth,@BackImage,@Url,@UrlType,@UrlParamValue,@WebSiteId,@WorkId,@Sort,@DelFlag)";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, setting);
    }

    public updateindexsetting(setting) {
        var strsql = "UPDATE CMS_IndexSetting\
                SET Title = @Title \
               ,Title_en = @Title_en \
               ,[Desc]= @Desc \
               ,Layout = @Layout \
               ,TextBackgroundColor =@TextBackgroundColor \
               ,TextWidth = @TextWidth \
               ,TotalWidth =@TotalWidth \
               ,BackImage = @BackImage \
               ,Url = @Url \
               ,UrlType = @UrlType \
               ,UrlParamValue = @UrlParamValue \
               ,WebSiteId = @WebSiteId \
               ,Sort = @Sort \
                 WHERE Id=@Id";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, setting);
    }

    public setsettingstatus(id, delflag) {
        var strsql = "UPDATE CMS_IndexSetting SET DelFlag=@delflag WHERE Id=@id";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { id: id, delflag: delflag });
    }

    public getcontextlistbywebsiteId(websiteId) {
        var strsql = "select * from CMS_BaseContextManage where NodeID   \
        in  (select NID from CMS_Node where WebsiteId=@websiteId and IsDel=0) and IsDel=0";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { websiteId: websiteId }, null);
    }

    public getnodelistbywebsiteId(websiteId) {
        var strsql = "select * from CMS_Node where WebsiteId=@websiteId and IsDel=0";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { websiteId: websiteId }, null);
    }
}